package cc.customer.web.center.saleManage;

import cc.customer.center.SaleCusManagerServices;
import cc.customer.common.domain.QueryRequest;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.utils.Constant;
import cc.customer.cusDynamic.domain.CusDynamic;
import cc.customer.cusDynamic.services.CusDynamicService;
import cc.customer.myCustomer.damain.CusResource;
import cc.customer.system.domain.MyUser;
import cc.customer.system.service.UserService;
import cc.customer.web.sys.controller.base.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="SaleManage")
public class SaleManageController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private SaleCusManagerServices customerServices;
    @Autowired
    private CusDynamicService cusDynamicService;

    /**销售管理---客户管理得加载**/
    @RequestMapping(value = "/getMyCustomerManageList",method = {RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> getMyCustomerManageList(CusResource cusResource, QueryRequest request){
        MyUser user = this.getCurrentUser();
        List<String> list = user.getAllAuthority();
        if (!list.contains(Constant.AMDIN_AUTORITY)){
            String usersids = cusResource.getUserid();
            if (StringUtils.isEmpty(usersids)){
                List<String> listUswers = new ArrayList<>();
                List<MyUser> listAllUser = userService.selectUserByDeptid(user);
                listAllUser.forEach(usesr->{
                    listUswers.add(usesr.getUserUuid());
                });
                String jonUser = StringUtils.join(listUswers,",");
                cusResource.setUserid(jonUser);
            }
        }
        String usersids = cusResource.getUserid();
        if (!StringUtils.isEmpty(usersids)){
            cusResource.setListUsers(Arrays.asList(usersids.split(",")));
        }
        return super.selectByPageNumSizeOrderBy(request, () -> this.customerServices.selectCusReManageList(cusResource,user));
    }

    /**销售管理----客户数量统计**/
    @RequestMapping(value = "/getCustomerIndex",method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo getCustomerIndex(CusResource cusResource){
        MyUser user = this.getCurrentUser();
        List<String> list = user.getAllAuthority();
        if (!list.contains(Constant.AMDIN_AUTORITY)){
            String usersids = cusResource.getUserid();
            if (StringUtils.isEmpty(usersids)){
                List<String> listUswers = new ArrayList<>();
                List<MyUser> listAllUser = userService.selectUserByDeptid(user);
                listAllUser.forEach(usesr->{
                    listUswers.add(usesr.getUserUuid());
                });
                String jonUser = StringUtils.join(listUswers,",");
                cusResource.setUserid(jonUser);
            }
        }
        String usersids = cusResource.getUserid();
        if (!StringUtils.isEmpty(usersids)){
            cusResource.setListUsers(Arrays.asList(usersids.split(",")));
        }
        return this.customerServices.selectCusCustomerList(cusResource);
    }
    
    /**销售管理---客户标签变化记录**/
    @RequestMapping(value = "/getCustomerLabelChange" ,method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo getCustomerLabelChange(CusDynamic cusDynamic){
        return this.cusDynamicService.getFollowList(cusDynamic);
    }



}
